package com.ewanghuiju.app.ui.my.activity;

import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.App;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.BaseActivity;
import com.ewanghuiju.app.base.RootActivity;
import com.ewanghuiju.app.base.contract.my.WalletCashContract;
import com.ewanghuiju.app.component.RxBus;
import com.ewanghuiju.app.model.bean.response.PintuanShopListResponBean;
import com.ewanghuiju.app.model.bean.response.RuleBean;
import com.ewanghuiju.app.model.bean.response.UserInfoResponBean;
import com.ewanghuiju.app.model.bean.response.ZfbInfoBean;
import com.ewanghuiju.app.model.dp.RealmHelper;
import com.ewanghuiju.app.model.event.SendEvent;
import com.ewanghuiju.app.presenter.my.WalletCashPresenter;
import com.ewanghuiju.app.util.AutoSoftUtils;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.ewanghuiju.app.util.MD5Util;
import com.ewanghuiju.app.util.StartActivityUtil;
import com.ewanghuiju.app.util.StringUtil;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.ewanghuiju.app.widget.popupwindow.CustomBasePopup;
import com.ewanghuiju.app.widget.popupwindow.PswKeyboardPopup;
import com.ewanghuiju.app.widget.pswkeyboard.OnPasswordInputFinish;
import com.gyf.immersionbar.ImmersionBar;
import com.muddzdev.styleabletoast.StyleableToast;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author sj  qq:751943509
 * @class 钱包提现页面
 * @time 2020/2/24
 */
public class WalletCashActivity extends BaseActivity<WalletCashPresenter> implements WalletCashContract.View {

    @BindView(R.id.iv_zfb_lab)
    ImageView ivZfbLab;
    @BindView(R.id.tv_zfb_name)
    TextView tvZfbName;
    @BindView(R.id.tv_zfb_account)
    TextView tvZfbAccount;
    @BindView(R.id.iv_edit)
    ImageView ivEdit;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.tv_to_bind)
    TextView tvToBind;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.et_input_money)
    EditText etInputMoney;
    @BindView(R.id.tv_can_cash)
    TextView tvCanCash;
    @BindView(R.id.tv_tip_info)
    TextView tvTipInfo;

    private int type = 1;
    private ZfbInfoBean mZfbInfoBean;

    @Override
    protected int getLayout() {
        return R.layout.activity_wallet_cash;
    }

    @Override
    protected void initEventAndData() {
//        if (type == 1) {
//            tvZfbName.setVisibility(View.GONE);
//            tvZfbAccount.setVisibility(View.GONE);
//            ivEdit.setVisibility(View.GONE);
//            tvToBind.setVisibility(View.VISIBLE);
//            ivRight.setVisibility(View.VISIBLE);
//        }
        RealmHelper realmHelper = App.getAppComponent().realmHelper();
        UserInfoResponBean userInfoResponBean = realmHelper.getUserInfoResponBean();
        if (userInfoResponBean != null) {
            tvCanCash.setText("当前可提现余额" + userInfoResponBean.getBalance() + "元");
        }
        LoadingDialogUtils.show(mContext);
        mPresenter.getAlipayinfo();
        mPresenter.getPtiguize("6");
    }

    @Override
    protected int getTitleIntText() {
        return R.string.cash_title;
    }

    @OnClick({R.id.tv_get_cash, R.id.tv_all_cash, R.id.layout_zfb_info})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {    //判断是否多次点击
            return;
        }
        switch (view.getId()) {
            case R.id.tv_get_cash:
                //提现请求
                AutoSoftUtils.hideInput(etInputMoney);
                if (mZfbInfoBean == null || TextUtils.isEmpty(mZfbInfoBean.getAlipay()) || TextUtils.isEmpty(mZfbInfoBean.getRealname())) {
                    StyleableToast.makeText(mContext, "请先绑定支付宝账号", Toast.LENGTH_SHORT, R.style.mytoast).show();
                    return;
                }
                String etMoney = etInputMoney.getText().toString();
                if (TextUtils.isEmpty(etMoney)) {
                    StyleableToast.makeText(mContext, "请输入提现金额", Toast.LENGTH_SHORT, R.style.mytoast).show();
                    return;
                }

                RealmHelper bindPwdRealmHelper = App.getAppComponent().realmHelper();
                UserInfoResponBean bindPwdUserInfoResponBean = bindPwdRealmHelper.getUserInfoResponBean();
                if (bindPwdUserInfoResponBean != null && bindPwdUserInfoResponBean.getIs_password() == 1) {
                    //已经绑定
                    //弹起密码键盘
                    PswKeyboardPopup pswKeyboardPopup = new PswKeyboardPopup(mContext);
                    pswKeyboardPopup.setPopupGravity(Gravity.BOTTOM);
                    pswKeyboardPopup.setOnFinishInput(new OnPasswordInputFinish() {
                        @Override
                        public void inputFinish(String password) {
                            pswKeyboardPopup.dismiss();
                            if (!StringUtil.isInteger(password)) {
                                StyleableToast.makeText(mContext, "密码输入格式错误", Toast.LENGTH_SHORT, R.style.mytoast).show();
                                pswKeyboardPopup.dismiss();
                                return;
                            }
                            String md5Pwd = MD5Util.getMD5(MD5Util.getMD5(password));
                            LoadingDialogUtils.show(mContext);
                            mPresenter.withdraw(etMoney, "alipay", mZfbInfoBean.getRealname(), mZfbInfoBean.getAlipay(), md5Pwd);
                        }
                    });
                    pswKeyboardPopup.showPopupWindow();
                } else {
                   //未绑定
                    nobindPwd();
                }
                break;
            case R.id.tv_all_cash:
                RealmHelper realmHelper = App.getAppComponent().realmHelper();
                UserInfoResponBean userInfoResponBean = realmHelper.getUserInfoResponBean();
                if (userInfoResponBean == null || TextUtils.isEmpty(userInfoResponBean.getBalance())) {
                    StyleableToast.makeText(mContext, "没有可提现余额", Toast.LENGTH_SHORT, R.style.mytoast).show();
                    return;
                }
                etInputMoney.setText(userInfoResponBean.getBalance());
                break;

            case R.id.layout_zfb_info:
                //修改支付宝信息
                new StartActivityUtil(mContext, ChangeBodyWithCodeActivity.class).putExtra(Constants.SETTING_TYPE, 3)
                        .startActivity(true);
                break;
            default:
                break;
        }
    }

    private CustomBasePopup customBasePopup;

    /**
     * 未绑定支付密码
     */
    public void nobindPwd() {
        //未绑定支付密码
        if (customBasePopup != null) {
            customBasePopup.showPopupWindow();
            return;
        }
        customBasePopup = new CustomBasePopup(mContext, "前往设置支付密码？", "", "",
                new CustomBasePopup.PopupWindowCallback() {
                    @Override
                    public void sureClickCallback() {
                        new StartActivityUtil(mContext, ChangeBodyWithCodeActivity.class).putExtra(Constants.SETTING_TYPE, 2)
                                .startActivity(true);
                    }

                    @Override
                    public void cancelClickCallback() {

                    }
                });
        customBasePopup.setPopupGravity(Gravity.CENTER);
        customBasePopup.showPopupWindow();
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .titleBar(mToolbar, true)
                .statusBarDarkFont(true)
                .init();
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void showContent(List<PintuanShopListResponBean> list) {

    }

    @Override
    public void showAlipayinfo(ZfbInfoBean zfbInfoBean) {
        //获取支付宝信息成功
        LoadingDialogUtils.dismissDialog_ios();
        this.mZfbInfoBean = zfbInfoBean;
        if (zfbInfoBean == null || TextUtils.isEmpty(zfbInfoBean.getAlipay()) || TextUtils.isEmpty(zfbInfoBean.getRealname())) {
            tvZfbName.setVisibility(View.GONE);
            tvZfbAccount.setVisibility(View.GONE);
            ivEdit.setVisibility(View.GONE);
            tvToBind.setVisibility(View.VISIBLE);
            ivRight.setVisibility(View.VISIBLE);
        } else {
            tvZfbName.setVisibility(View.VISIBLE);
            tvZfbAccount.setVisibility(View.VISIBLE);
            ivEdit.setVisibility(View.VISIBLE);
            tvToBind.setVisibility(View.GONE);
            ivRight.setVisibility(View.GONE);
            tvZfbName.setText(zfbInfoBean.getRealname());
            tvZfbAccount.setText(zfbInfoBean.getAlipay());
        }
    }

    @Override
    public void showAlipayinfoFail() {

    }

    @Override
    public void withdrawSuccess() {
        //提现成功
        LoadingDialogUtils.dismissDialog_ios();
//        StyleableToast.makeText(mContext, "提现申请已提交,请等待系统审核", Toast.LENGTH_SHORT, R.style.mytoast).show();
        //刷新用户信息
        RxBus.getDefault().post(new SendEvent("", Constants.TYPE_GET_USERINFO));
        onBackPressedSupport();
    }

    @Override
    public void showContentError() {

    }

    @Override
    public void showPtiguize(RuleBean bean) {
        //获取规则成功
        if (bean == null) {
            return;
        }
        if (!TextUtils.isEmpty(bean.getContent())) {
            tvTipInfo.setText(bean.getContent());
        }

    }
}
